package util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBToolEx {

	private static String driver;
	private static String url;
	private static String user;
	private static String pwd;

	static {

		Properties p = new Properties();
		try {
			p.load(DBTool.class.getClassLoader().
					getResourceAsStream("db.properties"));

			driver = p.getProperty("driver");
			url=p.getProperty("url");
			user=p.getProperty("user");
			pwd=p.getProperty("pwd");
			
			Class.forName(driver);
			
			
			
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("加载db.properties文件失败", e);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("注册驱动失败",e);
		
		}

	}

	public static Connection getConnection() throws SQLException{
		
		return DriverManager.getConnection(url,user,pwd);
		
		}
	
	public static void close(Connection conn){
		
		if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException("关闭数据库连接失败",e);
				}	
		}
				
	}
	
	
	
	
}
